package com.ayuan.blog.controller;

import com.alibaba.fastjson.JSONObject;
import com.ayuan.blog.bean.WxSignInLogInfo;
import com.ayuan.blog.common.WeChatUtil;
import com.ayuan.blog.common.wechat.base.WeChatBaseClient;
import com.ayuan.blog.common.wechat.message.WeChatMessageClient;
import com.ayuan.blog.common.wechat.user.WeChatUserClient;
import com.ayuan.blog.common.wechat.user.bean.WeChatUserInfo;
import com.ayuan.blog.domain.WxSignInLog;
import com.ayuan.blog.domain.WxUser;
import com.ayuan.blog.service.WxSignInLogService;
import com.ayuan.blog.service.WxUserService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * Created by xiaozhu on 16/5/8.
 */
@Controller
@Log4j2
public class SignInController {

    @Autowired
    private WxUserService wxUserService;
    @Autowired
    private WxSignInLogService wxSignInLogService;

    @RequestMapping(value = "/signin")
    public ModelAndView signIn(String code) {
        String openId = WeChatUtil.getOpenIdByCode(code);
//        String openId = "12";

        WxUser wxUser = wxUserService.getByOpenId(openId);
        if (null == wxUser) {
            WeChatUserInfo weChatUserInfo = WeChatUserClient.userInfo(WeChatUtil.getToken(), openId);
            wxUser = new WxUser();
            BeanUtils.copyProperties(weChatUserInfo, wxUser);
            wxUser.setUpdateTime(new Date());
            wxUserService.save(wxUser);
        }

        WxSignInLog todayWxSignInLog = wxSignInLogService.getTodaySignInLogByWxUserId(wxUser.getWxUserId());
        List<WxSignInLogInfo> wxSignInLogInfoList = wxSignInLogService.getTodaySignInLogInfoList();
        ModelAndView modelAndView = new ModelAndView("/checkin");
        modelAndView.addObject("nickName", wxUser.getNickname());
        modelAndView.addObject("wxUserId", wxUser.getWxUserId());
        modelAndView.addObject("todayWxSignInLog", todayWxSignInLog);
        modelAndView.addObject("wxSignInLogInfoList", wxSignInLogInfoList);

        return modelAndView;
    }

    @RequestMapping(value = "/dosignin", method = RequestMethod.GET)
    public ModelAndView doSignIn(HttpServletRequest request, HttpServletResponse response,
                           Long wxUserId, String comment) {
        WxSignInLog wxSignInLog = new WxSignInLog();
        wxSignInLog.setWxUserId(wxUserId);
        wxSignInLog.setComment(comment);
        wxSignInLog.setUpdateTime(new Date());
        wxSignInLog.setSignInDate(wxSignInLog.getUpdateTime());
        wxSignInLog.setSignInTime(wxSignInLog.getUpdateTime());
        wxSignInLogService.addWxSignInLog(wxSignInLog);

        WxUser wxUser = wxUserService.get(wxUserId);
        WxSignInLog todayWxSignInLog = wxSignInLogService.getTodaySignInLogByWxUserId(wxUser.getWxUserId());
        List<WxSignInLogInfo> wxSignInLogInfoList = wxSignInLogService.getTodaySignInLogInfoList();
        ModelAndView modelAndView = new ModelAndView("/checkin");
        modelAndView.addObject("nickName", wxUser.getNickname());
        modelAndView.addObject("wxUserId", wxUser.getWxUserId());
        modelAndView.addObject("todayWxSignInLog", todayWxSignInLog);
        modelAndView.addObject("wxSignInLogInfoList", wxSignInLogInfoList);

        WeChatMessageClient.messageCustomSend(WeChatUtil.getToken(),
                wxSignInLogService.getSignInMessage(wxUserId), wxUser.getOpenid());

        return modelAndView;
    }
}
